﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using System.Text;
using System.Linq;
using System.ComponentModel;

namespace BaiDuAPI
{
    public class maps
    {
        private double lati;
        private double loni ;

        public class MapUpdate
        {
            public HttpWebRequest AsyncRequest { get; set; }
            public HttpWebResponse AsyncResponse { get; set; }
        }

        public event PropertyChangedEventHandler PropertyChanged;


        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        public double Lati
        {
            get
            {
                return lati;
            }
            set
            {
                if (value != lati)
                {
                    lati = value;
                    //NotifyPropertyChanged("lati");
                }
            }
        }
        public double Loni
        {
            get
            {
                return loni;
            }
            set
            {
                if (value != loni)
                {
                    loni = value;
                   // NotifyPropertyChanged("loni");
                }
            }
        }
   

        public void querry(string p, string PlaceAPI)
        {
            string url = string.Format("http://api.map.baidu.com/geocoder?address={0}&output=xml&key={1}",p,PlaceAPI);
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
            MapUpdate forecastState = new MapUpdate();
            forecastState.AsyncRequest = wr;
            try
            {
                wr.BeginGetResponse(new AsyncCallback(HandleForecastResponse), forecastState);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
           
        }

        public void HandleForecastResponse(IAsyncResult asyncResult)
        {
            // get the state information
            MapUpdate forecastState = (MapUpdate)asyncResult.AsyncState;
            HttpWebRequest wr = (HttpWebRequest)forecastState.AsyncRequest;

            // end the async request
            forecastState.AsyncResponse = (HttpWebResponse)wr.EndGetResponse(asyncResult);

            System.IO.Stream streamResult;
            double newLat = 0.00;
            double newLon = 0.00;

            // create a temp collection for the new forecast information for each 
            // time period
            try
            {
                // get the stream containing the response from the async call
                streamResult = forecastState.AsyncResponse.GetResponseStream();

                // load the XML
                XElement xmlMap = XElement.Load(streamResult);

                // find the source element and retrieve the city and elevation information
                XElement xmlCurrent = xmlMap.Descendants("location").First();
                newLat = (double)(xmlCurrent.Element("lat"));
                newLon = (double)(xmlCurrent.Element("lng"));
                // copy the data over
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    Lati = newLat;
                    Loni = newLon;
                });
            }
            catch (FormatException)
            {
                return;
            }

        }
       
    }
}
